import com.powerfl.frm.taf.*;

private var _isActivated:int;
private var _isLeaved:int;
private var _isFullScreen:int;

private var _activeRender:Vector.<Function>;
private var _deactiveRender:Vector.<Function>;
private var _fixedRender:Vector.<Function>;

private var _activeLogic:Vector.<Function>;
private var _deactiveLogic:Vector.<Function>;
private var _fixedLogic:Vector.<Function>;

private var _mouseDown:Vector.<Function>;
private var _mouseUp:Vector.<Function>;
private var _resize:Vector.<Function>;
private var _leaved:Vector.<Function>;
private var _comeback:Vector.<Function>;
private var _activate:Vector.<Function>;
private var _deactivate:Vector.<Function>;

private var _fpsNext:int;
private var _fps:int;
private var _renderRate:Number;
private var _renderNext:int;

private var _runners:Vector.<Vector.<Function>>;

private function runnerInit():void{
	_renderNext = _fpsNext = getTimer();
	_fps = 1000;
	_renderRate = 0;
	
	_activeRender = new Vector.<Function>();
	_deactiveRender = new Vector.<Function>();
	_fixedRender = new Vector.<Function>();
	_activeLogic = new Vector.<Function>();
	_deactiveLogic = new Vector.<Function>();
	_fixedLogic = new Vector.<Function>();
	
	_mouseDown = new Vector.<Function>();
	_mouseUp = new Vector.<Function>();
	_resize = new Vector.<Function>();
	_leaved = new Vector.<Function>();
	_comeback = new Vector.<Function>();
	_activate = new Vector.<Function>();
	_deactivate = new Vector.<Function>();
	
	_runners = Vector.<Vector.<Function>>([_activeRender, _deactiveRender, _fixedRender, _activeLogic, _deactiveLogic, _fixedLogic, _resize, _leaved, _comeback, _activate, _deactivate, _mouseDown, _mouseUp]);
}
private function DOWN( $e:MouseEvent ):void{
	var key:*;
	$e.stopImmediatePropagation();
	for each( key in _mouseDown ){
		key();
	}
}
private function UP( $e:MouseEvent ):void{
	var key:*;
	$e.stopImmediatePropagation();
	for each( key in _mouseUp ){
		key();
	}
}
private function ACTIVATE ( $e:Event ):void{
	var key:*;
	$e.stopImmediatePropagation();
	if( _isFullScreen === 0 ){
//		$e.target.frameRate = 110; 
		_isActivated = -1;
		for each( key in _activate ){
			key();
		}
	}
}
private function DEACTIVATE( $e:Event ):void{
	var key:*;
	$e.stopImmediatePropagation();
	if( _isFullScreen === 0 ){
//		$e.target.frameRate = 0.1;
		_isActivated = 0;
		for each( key in _deactivate ){
			key();
		}
	}
}
private function MOUSE_LEAVE( $e:Event ):void{
	var key:*;
	$e.stopImmediatePropagation();
	_isLeaved = -1;
	for each( key in _leaved ){
		key();
	}
	$e.target.addEventListener( MouseEvent.MOUSE_MOVE, STAGE_IN );
}
private function STAGE_IN( $e:Event ):void{
	var key:*;
	$e.stopImmediatePropagation();
	_isLeaved = 0;
	for each( key in _comeback ){
		key();
	}
	$e.target.removeEventListener( $e.type, arguments.callee );
}
private function FULL_SCREEN( $e:FullScreenEvent ):void{
	var key:*;
	$e.stopImmediatePropagation();
	if( $e.fullScreen ){
		_isFullScreen = -1;
	}else{
		_isFullScreen = 0;
	}
}
private function RESIZE( $e:Event ):void{
	var key:*;
	$e.stopImmediatePropagation();
	if( _isFullScreen === 0 ){
		for each( key in _resize ){
			key();
		}
	}
}
private function ENTER_FRAME( $e:Event ):void{
	var key:*, time:uint, runner:*, check:int;
	$e.stopImmediatePropagation();
	++_fps;
	TIME = getTimer();
	if( TIME > _fpsNext ){
		_fpsNext = TIME + 1000;
		_renderRate = 1000 / _fps;
		_fps = 0;
	}
	check = 0;
	if( _isActivated ){
		if( _activeLogic.length ){
			for each( key in _activeLogic ){
				key();
			}
			check = 1;
		}
	}else if( _deactiveLogic.length ){
		for each( key in _deactiveLogic ){
			key();
		}
		check = 1;
	}
	if( _fixedLogic.length ){
		for each( key in _fixedLogic ){
			key();
		}
		check = 1;
	}
	if( check ){
		TIME = getTimer();
	}
	if( TIME > _renderNext ){
		STAGEX = STAGE.mouseX;
		STAGEY = STAGE.mouseY;
		if( _isActivated ){
			for each( key in _activeRender ){
				TIME = getTimer();
				key();
			}
		}else{
			for each( key in _deactiveRender ){
				TIME = getTimer();
				key();
			}
		}
		for each( key in _fixedRender ){
			TIME = getTimer();
			key();
		}
		TIME = getTimer();
		_renderNext = TIME + _renderRate; 
	}
}